<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span {
            display: inline-block;
            width: 250px;
            height: 30px;
            vertical-align: middle;
            line-height: 30px;
            padding-left: 15px;
        }

        .eror {
            color: red;
        }

        .right {
            color: green;
        }
    </style>
</head>

<body>
    <input type="text"><span></span>

    <script>
        const input = document.querySelector('input')
        const span = document.querySelector('span')
        // input的兄弟元素
        // span = input.nextElementSibling
        //    验证表单
        let yanzheng = /^[a-zA-Z0-9-_]{6,16}$/
        input.addEventListener('blur', function () {
            yanzheng.test(this.value) ? right() : eror()
            function right() {
                span.innerHTML = '正确'
                // className覆盖类名
                span.className = 'right'
            }
            function eror() {
                span.innerHTML = '错误'
                span.className = 'eror'
            }
        })

    </script>
</body>

</html>